package com.postek.coyote.postekwifi;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.postek.cdf.CDFPTKAndroid;
import com.postek.cdf.PcxImageConverter;


import java.io.InputStream;

/**
 * Created by Administrator on 2017/7/25 0025.
 */

public class printPhoto extends AppCompatActivity implements View.OnClickListener {

    private Button pfoto;
    private Button pt;
    private Button Lbt;
    private Button Sbt;
    private CDFPTKAndroid cdf = MainActivity.cdf;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.printphoto);
        pfoto= (Button) findViewById(R.id.bp);
       Lbt= (Button) findViewById(R.id.Lbt);
       Sbt=(Button) findViewById(R.id.sbt);
        pfoto.setOnClickListener(this);
        pt= (Button) findViewById(R.id.bt);
        pt.setOnClickListener(BT);
        Lbt.setOnClickListener(LBT);

        Sbt.setOnClickListener(SB);
    }

    @Override
    public void onClick(View v) {
//       Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.raw.lufei);
//       cdf.PTK_BmpGraphicsDownload("lufei",bitmap,0);
//       cdf.PTK_DrawPcxGraphics(20,20,"lufei");
//       cdf.PTK_PrintBMP(0,0,"lufei",bitmap,0);
//        InputStream is =getResources().openRawResource(R.raw.luf);
//        byte[] pcxFile=null;
//        try {
//            pcxFile = new byte[is.available()];
//            is.read(pcxFile);
//        }catch (Exception e){
//            try {
//                if(is !=null)
//                    is.close();
//            }
//            catch (Exception ex) { }
//        }
      //   cdf.PTK_EnableFLASH();
//        cdf.PTK_PcxGraphicsDownload("luf",pcxFile);
//        cdf.PTK_DrawPcxGraphics(0,0,"luf");
       // cdf.PTK_PrintLabel(1,1);

        cdf.PTK_DrawAndroidText(0,0,36,4,"N","postek","博学而笃志");
        cdf.PTK_DrawAndroidText(0,50,36,4,"R","postek1","切问而近思");
        cdf.PTK_PrintLabel(1,1);
        cdf.PTK_PcxGraphicsDel("postek");
        cdf.PTK_PcxGraphicsDel("postek1");


    }
    View.OnClickListener BT=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         //InputStream is =getResources().openRawResource(R.raw.luf);
            InputStream is =getResources().openRawResource(R.raw.luf);
        byte[] pcxFile=null;
        try {
            pcxFile = new byte[is.available()];
            is.read(pcxFile);
        }catch (Exception e){
            try {
                if(is !=null)
                    is.close();
            }
            catch (Exception ex) { }
        }
               cdf.PTK_EnableFLASH();
        cdf.PTK_PcxGraphicsDownload("luf",pcxFile);
        cdf.PTK_DrawPcxGraphics(0,0,"luf");
             cdf.PTK_PrintLabel(1,1);
             cdf.PTK_PcxGraphicsDel("luf");

        }
    };
    View.OnClickListener LBT=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setDither(true);
        p.setStyle(Paint.Style.FILL);
        p.setColor(Color.BLACK);
        p.setTextSize(100);
       // p.setStrokeWidth(2);
       // p.setAlpha(100);
        p.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

        String S="繁华如三千东流水";
        Rect rect=new Rect();
        p.getTextBounds(S,0,S.length(),rect);
        int  w=rect.width();
        int h=rect.height();

       Paint.FontMetricsInt fontMetrics = p.getFontMetricsInt();

        Bitmap BMP = Bitmap.createBitmap(w,h+fontMetrics.descent/2,Bitmap.Config.RGB_565);

        Canvas canvasTemp = new Canvas(BMP);
        canvasTemp.drawColor(Color.WHITE);
        canvasTemp.drawText(S,0,h,p);

        String pcxName = "postek";
        byte[] printContent = (new PcxImageConverter()).convertToPcx(BMP);
        Log.e("linbuff",new PcxImageConverter().convertToPcx(BMP).length+"");
        cdf.PTK_PcxGraphicsDownload(pcxName,printContent);
        cdf.PTK_DrawPcxGraphics(10,10,pcxName);
        cdf.PTK_PrintLabel(1,1);
        cdf.PTK_PcxGraphicsDel(pcxName);

        }
    };
    View.OnClickListener SB=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//            Paint p = new Paint();
//            p.setAntiAlias(true);
//            p.setDither(true);
//            p.setStyle(Paint.Style.FILL);
//            p.setColor(Color.WHITE);
//            p.setTextSize(50);
//            // p.setStrokeWidth(2);
//            // p.setAlpha(100);
//            p.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
//
//            String S="老夫聊发少年狂";
//            Rect rect=new Rect();
//            p.getTextBounds(S,0,S.length(),rect);
//            int  w=rect.width();
//            int h=rect.height();
//            Log.e("W", " "+w);
//            Log.e("H", " "+h);
//            Paint.FontMetricsInt fontMetrics = p.getFontMetricsInt();
//
//            //Bitmap BMP = Bitmap.createBitmap(w+5,h+fontMetrics.descent/2,Bitmap.Config.RGB_565);
//            Bitmap BMP = Bitmap.createBitmap(320,160,Bitmap.Config.RGB_565);
//            Log.e("W+5", " "+w+5);
//            Log.e("HB", " "+h+fontMetrics.descent/2);
//            Canvas canvasTemp = new Canvas(BMP);
//            canvasTemp.drawColor(Color.BLACK);
//           canvasTemp.drawText(S,(320-w)/2-fontMetrics.descent/4,(160+h)/2-fontMetrics.descent/2,p);
//            //canvasTemp.drawText(S,0,h,p);
//            String pcxName = "postek";
//            byte[] printContent = (new PcxImageConverter()).convertToPcx(BMP);
//            cdf.PTK_PcxGraphicsDownload(pcxName,printContent);
//            cdf.PTK_DrawPcxGraphics(20,20,pcxName);
//            cdf.PTK_PrintLabel(1,1);
//            cdf.PTK_PcxGraphicsDel(pcxName);
            graphicsGenerations("大漠孤烟直",50,400,320);
        }
    };

    private void graphicsGenerations(String Str, int size, int wide, int high) {
        // Paint p = new Paint();
        // //
        // p.setStrokeWidth(3);
        // p.setAntiAlias(true);
        // p.setDither(true);
        // p.setStyle(Paint.Style.FILL);
        // p.setColor(Color.WHITE);// Color.BLACK
        // p.setTextSize(size);
        // p.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

        // Rect rect = new Rect(100, 100, 500, 500);
        // p.getTextBounds(S, 0, S.length(), rect);
        // int w = rect.width() + 30;
        // int h = rect.height();
        // Paint.FontMetricsInt fontMetrics = p.getFontMetricsInt();
        // Bitmap BMP = Bitmap.createBitmap(w, h + fontMetrics.descent / 2,
        // Bitmap.Config.RGB_565);
        // Canvas canvasTemp = new Canvas(BMP);
        // canvasTemp.drawColor(Color.BLACK);// Color.WHITE
        // canvasTemp.drawText(S, 0, h, p);
        // return BMP;
        //

        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setDither(true);
        p.setStyle(Paint.Style.FILL);
        p.setColor(Color.WHITE);
        p.setTextSize(size);

        p.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

       // String S = "宝剑锋从磨砺出，梅花香自苦寒来";
        Rect rect = new Rect();
        p.getTextBounds(Str, 0, Str.length(), rect);
        int w = rect.width();
        int h = rect.height();
        Paint.FontMetricsInt fontMetrics = p.getFontMetricsInt();

        Bitmap BMP = Bitmap.createBitmap(wide, high, Bitmap.Config.RGB_565);
        Canvas canvasTemp = new Canvas(BMP);
        canvasTemp.drawColor(Color.BLACK);
        canvasTemp.drawText(Str, (wide - w) / 2 - fontMetrics.descent / 4, (high + h) / 2 - fontMetrics.descent / 2, p);
        String pcxName = "postek4";
        byte[] printContent = (new PcxImageConverter()).convertToPcx(BMP);
        cdf.PTK_PcxGraphicsDownload(pcxName,printContent);
        cdf.PTK_DrawPcxGraphics(0,3,pcxName);
        cdf.PTK_PrintLabel(1,1);
        cdf.PTK_PcxGraphicsDel(pcxName);
    }
}
